<template>
  <div>
    <ListTable ref="RefListTable" :loading="tableLoading" style="padding: 0 20px 40px 20px;" :table-data="tableData" :table-col="tableCol">
      <template slot="col1" slot-scope="scope">
        <el-button
          icon="el-icon-edit"
          :loading="scope.row.loadingEdit"
          size="mini"
          type="text"
          style="margin-left: 0px; margin-right: 10px;"
          @click="handleRowEdit(scope.row)"
        >编辑</el-button>
        <el-button
          :loading="scope.row.loadingDel"
          size="mini"
          type="text"
          icon="el-icon-delete"
          style="color: red; margin-left: 0px; margin-right: 10px;"
          @click="handleRowDel(scope.row)"
        >删除</el-button>
      </template>
    </ListTable>
    <FaceDetect ref="FaceDetect" @success="afterEditSuccess"></FaceDetect>
  </div>
</template>
<script>
  import listMixin from '@/mixins/list'
  import { faceDetectSetting } from '@/api/api'
  import FaceDetect from './component/FaceDetect'

  export default {
    components: {
      FaceDetect
    },
    mixins: [listMixin],
    data() {
      return {
        tableData: [],
        tableCol: [
          { type: 'index', label: '序号', width: '60px' },
          { prop: 'name', label: '设备名称' },
          { prop: 'ip', label: 'IP地址' },
          { prop: 'port', label: '端口' },
          { prop: 'pass', label: '通讯密码' },
          { prop: 'status', label: '状态' },
          { label: '操作', custom: 'col1' }
        ]
      }
    },
    methods: {
      // 请求列表数据
      async getList(query) {
        this.tableLoading = true
        try {
          let obj = query || { pageIndex: 1, pageSize: 10, orderBy: 'createdTime', orderByAscent: false }
          const { data: { items: resData, count, pageIndex } } = await faceDetectSetting('post', obj, false)
          this.tableData = []
          this.tableData = resData.map(item => {
            item.loadingEdit = false
            item.loadingDel = false
            return item
          })
          this.$nextTick(() => {
            this.$refs['RefListTable'].update({ page: pageIndex, count })
            this.tableLoading = false
          })
        } catch (err) {
          this.tableLoading = false
          this.tableData = []
        }
      },
      async afterEditSuccess() {
        this.getList(this.serchObj)
      },
      handleRowEdit(row) {
        let objData = {
          // 进入哪一个程序
          status: 'edit',
          // 源数据
          row: row,
          // 源id
          id: row.id,
          // 查询参数
          searchData: {
            url: '/FaceDetectSetting/' + row.id,
            method: 'GET',
            params: null
          }
        }
        this.$refs['FaceDetect'].openOne(objData)
      },
      async delRowReq(row) {
        try {
          let obj = { id: row.id, value: null }
          await faceDetectSetting('delete', obj, true)
          row.loadingDel = false
          this.getList(this.serchObj)
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        } catch (err) {
          row.loadingDel = false
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        }
      },
      handleRowDel(row) {
        row.loadingDel = true
        this.$_delConfirm(
          '该设备',
          () => {
            this.delRowReq(row)
          },
          () => {
            row.loadingDel = false
          }
        )
      }
    }
  }
</script>
<style lang="scss" scoped>
  .wid {
    width: 300px;
  }
</style>
